package com.theme.passport.service.theme.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.theme.common.mvc.utils.PageUtils;
import com.theme.common.mvc.utils.Query;
import com.theme.common.office.EchartsHandler;
import com.theme.passport.dao.theme.SearchHistoryDao;
import com.theme.passport.entity.theme.SearchHistoryEntity;
import com.theme.passport.service.theme.SearchHistoryService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;


@Service("searchHistoryService")
public class SearchHistoryServiceImpl extends ServiceImpl<SearchHistoryDao, SearchHistoryEntity> implements SearchHistoryService {


    @Autowired
    private SearchHistoryDao searchHistoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String userId = (String) params.get("userId");
        IPage<SearchHistoryEntity> page = this.page(
                new Query<SearchHistoryEntity>().getPage(params),
                new QueryWrapper<SearchHistoryEntity>()
                        .eq(StringUtils.isNotBlank(userId), "user_id", userId)
                        .orderByDesc("created")
        );
        return new PageUtils(page);
    }

    @Override
    public List<Map<String, Object>> querySearchHotKeyword(Map<String, Object> params) {
        return searchHistoryDao.querySearchHotKeyword(params);
    }

    @Override
    public Map<String, Object> searchHistoryService(Byte dateType) {
        List<Map<String, Object>> searchKeyWorldLineChart = null;
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Map<String, Object> params = new HashMap<>();
            if (dateType == null){
                searchKeyWorldLineChart = searchHistoryDao.querySearchHotKeywordLimit(params);
                return completionMapData(searchKeyWorldLineChart);
            }
            Date startTime = EchartsHandler.getStartTimeByDateType(dateType);
            Date endTime = EchartsHandler.getEndTimeByDateType(dateType);
            params.put("startTime", formatter.format(startTime));
            params.put("endTime", formatter.format(endTime));
            if (dateType == 1) {
                //1、按当日查询-每个小时段
                searchKeyWorldLineChart = searchHistoryDao.querySearchHotKeywordLimit(params);
            } else if (dateType == 2) {
                //2、按天查询-近七天
                searchKeyWorldLineChart = searchHistoryDao.querySearchHotKeywordLimit(params);;
            } else if (dateType == 3) {
                //3、按天查询-近30天
                searchKeyWorldLineChart = searchHistoryDao.querySearchHotKeywordLimit(params);;
            } else if (dateType == 4) {
                //4、按天查询-当月每一天
                searchKeyWorldLineChart = searchHistoryDao.querySearchHotKeywordLimit(params);
            } else if (dateType == 5) {
                //5、按月查询-当年每个月
                searchKeyWorldLineChart = searchHistoryDao.querySearchHotKeywordLimit(params);
            }else {
                searchKeyWorldLineChart = searchHistoryDao.querySearchHotKeywordLimit(params);
            }
            return completionMapData(searchKeyWorldLineChart);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private Map<String, Object> completionMapData(List<Map<String, Object>> lineChartData) {
        Map<String, Object> result = new HashMap<>();
        List<String> keywordList = new ArrayList<>();
        List<Integer> totalList = new ArrayList<>();
        if (lineChartData.size() > 0) {
            for (Map<String, Object> m : lineChartData) {
                String keyword = m.get("keyword").toString();
                Integer total = Integer.valueOf(m.get("num").toString());
                keywordList.add(keyword);
                totalList.add(total);
            }
            String[] keywords = keywordList.toArray(new String[keywordList.size()]);
            Integer[] totals = totalList.toArray(new Integer[totalList.size()]);
            result.put("lineDate",keywords);
            result.put("lineCount",totals);
            return result;
        }
        return result;
    }


}